﻿using System;

namespace Dgg.LearnYourTools.NUnit.C_Assertions.Support
{
	[Serializable]
	public class Range<T> where T : IComparable<T>
	{
		private readonly T _lowerBound;
		private readonly T _upperBound;

		public Range(T lowerBound, T upperBound)
		{
			if (lowerBound.IsMoreThan(upperBound))
			{
				throw new ArgumentOutOfRangeException("upperBound", upperBound, 
					"The start value of the range must not be greater than its end value.");
			}

			_lowerBound = lowerBound;
			_upperBound = upperBound;
		}

		public T LowerBound { get { return _lowerBound; } }

		public T UpperBound { get { return _upperBound; } }

		public bool Contains(T item)
		{
			return item.IsAtLeast(_lowerBound) && item.IsAtMost(_upperBound);
		}
	}

	internal static class ComparableExtensions
	{
		internal static bool IsMoreThan<T>(this T instance, T value) where T : IComparable<T>
		{
			return instance.CompareTo(value) > 0;
		}

		internal static bool IsAtLeast<T>(this T instance, T value) where T : IComparable<T>
		{
			return instance.CompareTo(value) >= 0;
		}

		internal static bool IsAtMost<T>(this IComparable<T> instance, T value)
		{
			return instance.CompareTo(value) <= 0;
		}
	}
}
